$(document).ready(function(){
	/*列表控件*/
	$('#user_vm_tt').datagrid({
		title: '防毒墙用户数据'
		, fit: true
		, url: 'user-vm.php?method=GetData'
		, pagination: true /*底部分页栏*/
		, pageList: [100, 200, 500]
		, loadMsg: '正在加载数据，请稍等 ...'
		, columns:[[
				{field:'ck', width:60, checkbox:true}
				, {field:'id', title:'ID序号', width:60}
				, {field:'name', title:'用户单位', width:100}
				, {field:'email', title:'联系邮箱', width:200}
				, {field:'phone', title:'联系手机', width:80, align: 'center'}
				, {field:'lastdt', title:'到期时间', width:120, align:'right'} 
				, {field:'promptdt', title:'最后提醒时间', width:120, align:'center'} 
				, {field:'sem', title:'发送邮件(成功/失败)', width:120}
				, {field:'sms', title:'发送短信(成功/失败)', width:120}
				, {field:'mms', title:'发送短信(成功/失败)', width:120}
				, {field:'#id', title:'操作', align:'center', width:80, formatter:function(value, rowData){
					return "<a href=\"javascript:void(0)\" onclick=\"$('#user_vm_tt').datagrid('options').itemdelete(" + rowData.id + ")\">删除</a>"
						+ " | <a href=\"javascript:void(0)\" onclick=\"$('#user_vm_tt').datagrid('options').itemmodify(" + rowData.id + ")\">修改</a>";
				}}
			]]  
		, toolbar:[{
				id:'user_vm_delete'
				, text: '删除选中'
				, iconCls: 'icon-remove'
				, handler: function(){
					/*删除选中用户*/
					var sels = $('#user_vm_tt').datagrid('getSelections');
					if(!sels.length){
						top.$.messager.alert('操作错误', '请先择要删除的项', 'error');
						return;
					}
					/*组合要删除的项*/
					var ids = ';';
					for(it in sels){
						ids += sels[it].id + ';';
					}
					/*发送删除的项*/
					$('#user_vm_tt').datagrid('options').itemdelete(ids);
				}
			},{
				id:'user_vm_clear'
				, text: '清空数据'
				, iconCls: 'icon-remove'
				, handler: function(){
					/*清空所有用户*/
					top.$.messager.confirm('确认操作', '确实要删除所有的防毒墙用户信息吗?'
						, function(b){
							if(!b) return;
							$.ajax({type:'POST'
								, url: 'user-vm-action.php'
								, data: 'type=delall'
								, dataType: 'json'
								, error: function(XMLHttpRequest, textStatus, errorThrown){
									top.$.messager.alert('错误', '请求失败:' + errorThrown, 'error');
								}
								,success: function(msg){
									if(msg.success){
										top.$.messager.alert('操作成功', '删除有用用户成功', 'info');
										$('#user_vm_tt').datagrid('load');
									} else {
										top.$.messager.alert('操作失败', '删除所用有用户失败:'+msg.msg, 'error');
									}
								}
							});
						}
					);
				}
			},'-',{
				id: 'user_vm_add'
				, text: '添加新用户'
				, iconCls: 'icon-add'
				, handler:function(){
					$('#user_vm_add_ff').form('clear');
					$('form[@id="user_vm_add_ff"] input[name="type"]').val("add");
					$('#user_vm_add_dd').dialog({title: "添加新的防毒墙用户"});
					$('#user_vm_add_dd').dialog('open');
				}
			},{
				id: 'user_vm_input'
				, text: '导入外部数据'
				, iconCls: 'icon-reload'
				, handler: function(){
					$('#user_vm_import_ff').form('clear');
					$('#user_vm_import_p').progressbar('setValue', '0');   
					$('#user_vm_import_dd').dialog('open');
				}
			}]
		, itemdelete: function(ids){
			/* 删除操作 */
			top.$.messager.confirm('确认操作', '确实要删除用户[' + ids + ']吗?'
						, function(b){
							if(!b) return;
							$.ajax({type:'POST'
								, url:'user-vm-action.php'
								, data:'type=delselect&ids=' + ids
								, dataType: 'json'
								, error: function(XMLHttpRequest, textStatus, errorThrown){
									top.$.messager.alert('错误', '请求失败:' + errorThrown, 'error');
								}
								, success:function(msg){
									if(msg.success){
										top.$.messager.alert('操作成功', '删除用户成功', 'info');
										$('#user_vm_tt').datagrid('load');
									} else {
										alert(outobject(msg));
										top.$.messager.alert('操作失败', '删除用户失败:'+outobject(msg), 'error');
									}
								}
							});	
						}
					);
		}
		, itemmodify: function(id){
			/*修改操作*/
			data = $('#user_vm_tt').datagrid('getData');
			if(!data) return;
			$('#user_vm_add_ff').form('clear');
			$('form[@id="user_vm_add_ff"] input[name="type"]').val("modify");
			$('form[@id="user_vm_add_ff"] input[name="id"]').val(id);
			try{
				for(var i = 0; i < data.rows.length; i++){
					if(id == data.rows[i].id){
						$('form[@id="user_vm_add_ff"] input[name="name"]').val(data.rows[i].name);
						$('form[@id="user_vm_add_ff"] input[name="phone"]').val(data.rows[i].phone);
						$('form[@id="user_vm_add_ff"] input[name="lasttime"]').val(data.rows[i].lastdt);
						$('form[@id="user_vm_add_ff"] input[name="email"]').val(data.rows[i].email);
						break;
					}
				}
			} catch (e) {
				alert(e);
				return;
			}
			// 打开对话框
			$('#user_vm_add_dd').dialog({title: "修改用户id为" + id + "的用户信息"});
			$('#user_vm_add_ff').form('validate')
			$('#user_vm_add_dd').dialog('open');
		}
	});
	
	/*添加操作*/
	$('#user_vm_add_ff').form({
		url:'user-vm-action.php'
		, onSubmit: function(){
			if( !$('#user_vm_add_ff').form('validate') ){
				top.$.messager.alert('错误', '请输入完整的信息', 'error');
				return false; // 验证不通过
			}
		}
		, error: function(XMLHttpRequest, textStatus, errorThrown){
			top.$.messager.alert('错误', '提交添加失败:' + errorThrown, 'error');
		}
		, success: function(data){
			var msg = decodeJSON(data);
			var type = $('#user_vm_add_dd').dialog("options").title;
			if(msg.success){
				top.$.messager.alert('操作成功', type + '成功', 'info');
				$('#user_vm_add_dd').dialog('close');
				if(String(type).indexOf('id') > 0)
					$('#user_vm_tt').datagrid('reload');
				else
					$('#user_vm_tt').datagrid('load');
			} else {			
				top.$.messager.alert('操作失败', type + '户失败:'+msg.msg, 'error');
			}
		}
	});
	/*导入防毒墙用户*/
	$('#user_vm_import_ff').form({
		url:'user-vm-upload.php'
		, onSubmit: function(){
			if( 0 == user_vm_import_ff.file.value.length ){
				top.$.messager.alert('错误', '请选择要上传的文件', 'error');
				return false; // 验证不通过
			}
		}
		, error: function(XMLHttpRequest, textStatus, errorThrown){
			top.$.messager.alert('错误', '提交添加失败:' + errorThrown, 'error');
		}
		, success: function(data){
			var msg = decodeJSON(data);
			if(msg.success){
				top.$.messager.alert('操作成功', '导入新用户成功:' + msg.msg, 'info');
				$('#user_vm_import_dd').dialog('close');
				$('#user_vm_tt').datagrid('load');
			} else {
				top.$.messager.alert('操作失败', '导入新用户败:'+msg.msg, 'error');
			}
		}
	});
});